<template>
  <div>
    <div>
      <el-card class="user-list" shadow="never">
        <el-link
          class="user-item"
          icon="el-icon-chat-round"
          v-for="u in list"
          :key="u.id"
          :underline="false"
          @click="getUserInfo(u)"
        >{{u.name}}</el-link>
      </el-card>
    </div>
  </div>
</template>
<script>
import { UserInfoReq, MsgHeader, MsgType } from "../../security_msg_pb";
const fs = require("fs");
export default {
  name: "UserList",
  props: ["list"],
  created() {},
  data() {
    return {
      users: null
    };
  },
  methods: {
    getUserInfo(user) {
      fs.open("data/.shadow/pub/" + user.id + ".pem", (err, fd) => {
        if (err) {
          console.log(err);
          let userInfoReq = new UserInfoReq();
          userInfoReq.setAlias(user.name);
          userInfoReq.setIsNeedKey(true);
          let userInfoReqBuffer = userInfoReq.serializeBinary();
          let msgHeader = new MsgHeader();
          msgHeader.setMsgSize(userInfoReqBuffer.length);
          msgHeader.setMsgType(MsgType.USER_INFO_REQ);
          msgHeader.setFrom(this.$store.state.User.userID);
          msgHeader.setTo(0);
          msgHeader.setToken(this.$store.state.User.userToken);
          let headerBuffer = msgHeader.serializeBinary();
          let packageHeader = Buffer.alloc(8);
          packageHeader.write("AB47");
          packageHeader.writeInt32LE(headerBuffer.length, 4);
          const packageBuffer = Buffer.concat([
            packageHeader,
            headerBuffer,
            userInfoReqBuffer
          ]);
          this.$socket.write(packageBuffer);
        }
        this.$emit("choose", user.name, user.id);
      });
    }
  },
  watch: {}
};
</script>
<style scoped>
.user-list {
  height: 540px;
}
.user-item {
  margin-top: 5px;
}
</style>